import matplotlib.pyplot as plt # 导入绘图库
figure, axis = plt.subplots(figsize=(7.0, 3.8)) # 创建适配幻灯片的横向画布
x_lower, x_upper = x_now['book_to_market_ratio_lf'].quantile([0.01, 0.99]) # 横轴1%—99%分位限
y_lower, y_upper = x_now['ep_ratio_ttm'].quantile([0.01, 0.99]) # 纵轴1%—99%分位限
axis.set_xlim(x_lower, x_upper) # 设横轴范围压缩极端值
axis.set_ylim(y_lower, y_upper) # 设纵轴范围压缩极端值
scatter_handle = axis.scatter(x_now['book_to_market_ratio_lf'], x_now['ep_ratio_ttm'], c=kmeans_now.labels_, cmap='viridis', s=12, alpha=0.5) # 公司散点按簇着色
axis.scatter(kmeans_now.cluster_centers_[:, 1], kmeans_now.cluster_centers_[:, 0], c='#EC232A', marker='X', s=220, edgecolors='black', linewidths=1.5) # 红色X标记质心(列序:EP,BM,股息,对数市值)
axis.set_xlabel('B/M ratio (z-score)') # 横轴为账面市值比标准化值
axis.set_ylabel('EP ratio (z-score)') # 纵轴为盈利收益率标准化值
axis.grid(True, alpha=0.3) # 显示浅网格
figure.colorbar(scatter_handle, ax=axis, label='Cluster') # 添加簇编号颜色条
figure.tight_layout() # 自动收紧边距
plt.show() # 显示图形